<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/1
 * Time: 23:52
 */
namespace Admin\Controller;
use Think\Controller;

class DocController extends CommonController{
    public function add(){
        if(IS_POST){
            $post=I('post.');
            $model=D("Doc");
            $result=$model -> saveDate($post,$_FILES['file']);
            if($result){
                $this->success('添加成功',U('showList'),3);
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
        
    }

    public function showList(){
        $model=M('Doc');
        $result=$model->select();
        $this -> assign('result',$result);
        $this->display();
    }

    //download下载
    public function download(){
        $id=I('get.id');
        $data=M('Doc')->find($id);
        $file=WORKING_PATH.$data['filepath'];
        header("Content-type: application/octet-stream");
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header("Content-Length: ". filesize($file));
        readfile($file);
    }

    public function showContent(){
        $id=I('get.id');
        $data=M('Doc')->find($id);
        echo htmlspecialchars_decode($data['content']);
    }

    public function edit(){
        if(IS_POST){
            $post=I('post.');
            $model=D('Doc');
            $result=$model->updateData($post,$_FILES['file']);
            if($result){
                $this->success('修改成功！',U('showList'),3);
            }else{
                $this->error('保存失败！');
            }
        }else{
            $id=I('get.id');
            $data=M('Doc')->find($id);
            $this->assign('data',$data);
            $this->display();
        }
        
    }

    public function date(){
        header('Access-Control-Allow-Origin:*');
        $data=I('post.');
        dump($data);
        $model=M('Doc');
        $result=$model->select();
        // $this->ajaxReturn(json_encode($result));
        echo json_encode($result);
    }

}